from building.constants import WINS, FINISHED
from django.db import models


from users.models import UserInfo


class Category(models.Model):
    """
    工程类别
    建筑,市政,机电,装修,劳务
    """
    id = models.IntegerField(verbose_name='类型编号', primary_key=True)
    name = models.CharField(verbose_name="类型名称", max_length=32)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '工程类别'
        verbose_name_plural = verbose_name
        ordering = ['id']


class BidType(models.Model):
    """
    标书类型：部队,电标,纸标
    """
    id = models.IntegerField(verbose_name='类型编号', primary_key=True)
    name = models.CharField(verbose_name='类型名称', max_length=24)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '标书类型'
        verbose_name_plural = verbose_name
        ordering = ['id']


class DocType(models.Model):
    """档案资料类型"""
    id = models.IntegerField(verbose_name='类型编号', primary_key=True)
    name = models.CharField(verbose_name="类型名称", max_length=32)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '资料类型'
        verbose_name_plural = verbose_name
        ordering = ['id']


class Project(models.Model):
    """工程登记表"""
    name = models.CharField(verbose_name="工程名称", max_length=64, blank=True, null=True)
    accept_date = models.DateField(verbose_name='受理日期', blank=True, null=True)
    accept_person = models.CharField(verbose_name='受理人', max_length=24, blank=True, null=True)
    introducer = models.CharField(verbose_name='介绍人', max_length=24, blank=True, null=True)
    contact_person = models.CharField(verbose_name='联系人', max_length=24, blank=True, null=True)
    contact_tel = models.CharField(verbose_name='联系电话', max_length=24, blank=True, null=True)
    bid_type = models.ForeignKey(BidType, verbose_name='标书类型', null=True, blank=True, db_constraint=False, on_delete=models.SET_NULL)
    category = models.ForeignKey(Category, verbose_name='工程类型', null=True, blank=True, db_constraint=False, on_delete=models.SET_NULL)
    is_win = models.IntegerField(verbose_name='是否中标', default=0, blank=True, null=True, choices=WINS)
    enter_date = models.DateField(verbose_name='报名时间', blank=True, null=True)
    open_time = models.DateTimeField(verbose_name='开标时间', null=True, blank=True)
    architect = models.CharField(verbose_name='二级建造师', max_length=24, blank=True, null=True)
    auth_person = models.CharField(verbose_name='授权委托人', max_length=24, blank=True, null=True)
    remark = models.CharField(verbose_name='备注', max_length=240, blank=True, null=True)
    quali_recv_fee = models.FloatField(verbose_name='资质费用(应收)', blank=True, null=True, default=0)
    recv_remark = models.CharField(verbose_name='应收说明', max_length=64, blank=True, null=True)
    quali_pay_fee = models.FloatField(verbose_name='资质费用(应付)', blank=True, null=True, default=0)
    pay_remark = models.CharField(verbose_name='应付说明', max_length=64, blank=True, null=True)
    costs = models.FloatField(verbose_name='工程造价', blank=True, null=True, default=0)
    finish = models.IntegerField(verbose_name='完结', blank=True, null=True, choices=FINISHED)
    user = models.ForeignKey(UserInfo, verbose_name='经办人', blank=True, null=True, on_delete=models.SET_NULL,
                             db_constraint=False)
    merit = models.FloatField(verbose_name='绩效', blank=True, null=True, default=0)
    merit_desc = models.CharField(verbose_name='绩效说明', max_length=120, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = '工程登记表'
        verbose_name_plural = verbose_name
        ordering = ['-id']


class Archive(models.Model):
    """工程档案"""
    BID = (
        (0, '否'),
        (1, '是'),
    )
    code = models.CharField(verbose_name='档案号', max_length=12, null=True, blank=True)
    project = models.ForeignKey(Project, verbose_name='工程ID', related_name='archives', blank=True, null=True, on_delete=models.SET_NULL, db_constraint=False)
    project_name = models.CharField(verbose_name='工程名称', max_length=256, blank=True, null=True)
    archive_date = models.DateField(verbose_name='建档日期', blank=True, null=True)
    is_bid = models.IntegerField(verbose_name='是否投标', blank=True, null=True, choices=BID)
    category = models.ForeignKey(Category, verbose_name='工程类型', null=True, blank=True, db_constraint=False,
                                 on_delete=models.SET_NULL)
    costs = models.FloatField(verbose_name='工程造价', blank=True, null=True, default=0)
    duty_man = models.CharField(verbose_name='负责人', max_length=24, blank=True, null=True)
    finish = models.IntegerField(verbose_name='是否完结', blank=True, null=True, choices=FINISHED)
    user = models.ForeignKey(UserInfo, verbose_name='经办人', blank=True, null=True, on_delete=models.SET_NULL,
                             db_constraint=False)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.code

    class Meta:
        verbose_name = '工程档案'
        verbose_name_plural = verbose_name
        ordering = ['-id']


class Document(models.Model):
    """档案资料内容"""
    archive = models.ForeignKey(Archive, verbose_name='工程档案', related_name='documents', blank=True, null=True, on_delete=models.CASCADE, db_constraint=False)
    doc_type = models.ForeignKey(DocType, verbose_name='资料类型', blank=True, null=True, db_constraint=False, on_delete=models.SET_NULL)
    name = models.CharField(verbose_name='文件名', max_length=120, blank=True, null=True)
    content = models.FileField(verbose_name='文件',  upload_to='document/', null=True, blank=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.archive

    class Meta:
        verbose_name = '档案资料'
        verbose_name_plural = verbose_name
        ordering = ['-id']


class Deposit(models.Model):
    """保证金"""
    project = models.ForeignKey(Project, verbose_name='工程名称', related_name='deposits', blank=True, null=True,
                                on_delete=models.SET_NULL, db_constraint=False)
    pay_date = models.DateField(verbose_name='交保证金日期', blank=True, null=True)
    pay_money = models.FloatField(verbose_name='保证金额', blank=True, null=True, default=0)
    refund_date = models.DateField(verbose_name='退保证金日期', blank=True, null=True)
    refund_money = models.FloatField(verbose_name='退保金额', blank=True, null=True, default=0)
    contact_man = models.CharField(verbose_name='联系人', max_length=24, blank=True, null=True)
    remark = models.CharField(verbose_name='备注', max_length=50, blank=True, null=True)
    explain = models.CharField(verbose_name='退保说明', max_length=240, blank=True, null=True)
    user = models.ForeignKey(UserInfo, verbose_name='经办人', blank=True, null=True, on_delete=models.SET_NULL,
                             db_constraint=False)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.project

    class Meta:
        verbose_name = '档案资料'
        verbose_name_plural = verbose_name
        ordering = ['-id']


class BidBusiness(models.Model):
    """
    投标业务
    """
    project = models.CharField(verbose_name='工程名称', max_length=256, blank=True, null=True)
    bid_date = models.DateField(verbose_name="时间", blank=True, null=True)
    bid_type = models.ForeignKey(BidType, verbose_name='类型', null=True, blank=True, db_constraint=False,
                                 on_delete=models.SET_NULL)
    amount = models.IntegerField(verbose_name='数量', blank=True, null=True)
    fees = models.FloatField(verbose_name='费用', blank=True, null=True)
    manager = models.CharField(verbose_name='项目经理', max_length=24, blank=True, null=True)
    broker = models.CharField(verbose_name='居间人', max_length=24, blank=True, null=True)
    broker_fees = models.FloatField(verbose_name='居间费', blank=True, null=True)
    persons = models.CharField(verbose_name='参与人', max_length=120, blank=True, null=True)
    remark = models.CharField(verbose_name='备注', max_length=64, blank=True, null=True)
    user = models.ForeignKey(UserInfo, verbose_name='经办人', blank=True, null=True, on_delete=models.SET_NULL,
                             db_constraint=False)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.project

    class Meta:
        verbose_name = '投标业务明细'
        verbose_name_plural = verbose_name
        ordering = ['-id']


class BidDocument(models.Model):
    """投标资料"""
    DOCTYPE = (
        (1, '招标文件'),
        (2, '投标文件'),
        (3, '计价'),
    )
    bid = models.ForeignKey(BidBusiness, verbose_name='投标业务', related_name="documents", on_delete=models.CASCADE, blank=True, null=True, db_constraint=False)
    doc_type = models.IntegerField(verbose_name='资料类型', blank=True, null=True, choices=DOCTYPE)
    name = models.CharField(verbose_name='文件名', max_length=120, blank=True, null=True)
    content = models.FileField(verbose_name='文件', upload_to='bids/', null=True, blank=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.bid

    class Meta:
        verbose_name = '投标资料'
        verbose_name_plural = verbose_name
        ordering = ['-id']


class BidInspector(models.Model):
    """检查人员"""
    CATEGORY = (
        (1, '土建'),
        (2, '安装'),
        (3, '投标文件'),
    )
    bid = models.ForeignKey(BidBusiness, verbose_name='投标业务', related_name='inspectors', on_delete=models.CASCADE, blank=True, null=True, db_constraint=False)
    category = models.IntegerField(verbose_name='人员类型', blank=True, null=True, choices=CATEGORY)
    name = models.CharField(verbose_name='姓名', max_length=32, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.bid

    class Meta:
        verbose_name = '投标资料'
        verbose_name_plural = verbose_name
        ordering = ['-id']


class ProjectFinance(models.Model):
    """工程项目财务综合统计"""
    YESNO = (
        (1, '是'),
        (0, '否'),
    )
    project = models.ForeignKey(Project, verbose_name='工程名称', related_name='finances', blank=True, null=True,
                                on_delete=models.SET_NULL, db_constraint=False)
    company = models.CharField(verbose_name='单位名称', max_length=64, blank=True, null=True)
    contract_accounts = models.FloatField(verbose_name='合同价格', blank=True, null=True)
    # balance_accounts = models.FloatField(verbose_name='完工结算价', blank=True, null=True)
    # bill = models.IntegerField(verbose_name='发票', blank=True, null=True, choices=YESNO)
    # checkout = models.IntegerField(verbose_name='结账', blank=True, null=True, choices=YESNO)
    former_bill = models.FloatField(verbose_name='往年开发票金额', blank=True, null=True)
    current_bill = models.FloatField(verbose_name='本年开发票金额', blank=True, null=True)
    remark = models.CharField(verbose_name='备注', max_length=64, blank=True, null=True)
    manager = models.CharField(verbose_name='项目经理', max_length=24, blank=True, null=True)
    telephone = models.CharField(verbose_name='联系方式', max_length=24, blank=True, null=True)
    user = models.ForeignKey(UserInfo, verbose_name='经办人', blank=True, null=True, on_delete=models.SET_NULL,
                             db_constraint=False)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.project

    class Meta:
        verbose_name = '项目综合情况统计'
        verbose_name_plural = verbose_name
        ordering = ['-id']


class FundsDetail(models.Model):
    """工程款收入支出明细"""
    FUND_TYPE = (
        (0, '已收甲方工程款'),
        (1, '已付项目部工程款'),
    )
    finance = models.ForeignKey(ProjectFinance, verbose_name='工程名称', related_name='details', blank=True, null=True, on_delete=models.CASCADE, db_constraint=False)
    fund_class = models.IntegerField(verbose_name='工程款类别', blank=True, null=True, choices=FUND_TYPE)
    fund_date = models.DateField(verbose_name='支付时间', blank=True, null=True)
    money = models.FloatField(verbose_name='金额(元)', blank=True, null=True)
    remark = models.CharField(verbose_name='备注', max_length=120, blank=True, null=True)
    update_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    add_time = models.DateTimeField(verbose_name='添加时间', auto_now_add=True)

    def __str__(self):
        return self.finance

    class Meta:
        verbose_name = '项目综合情况统计'
        verbose_name_plural = verbose_name
        ordering = ['-id']
